package com.pactera.madp.cp.controller.oms.contact;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.workcontacttag.WorkContactTagAddDTO;
import com.pactera.madp.cp.api.dto.workcontacttag.WorkContactTagsAddDTO;
import com.pactera.madp.cp.api.vo.ContactTagDetailVO;
import com.pactera.madp.cp.api.vo.ContactTagIndexVO;
import com.pactera.madp.cp.api.vo.ContactTagVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.IWorkContactTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: yangpengwei
 * @time: 2020/12/14 6:21 下午
 * @description 客户标签管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/workcontacttag")
@Api(value = "workcontacttag", tags = "客户标签管理")
public class WorkContactTagController {

    private IWorkContactTagService workContactTagService;

    /**
     * 获取标签列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取标签列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="标签组id",dataType="Integer")
    })
    public R<Page<List<ContactTagIndexVO>>> getTagList(Integer groupId, Page page) {
        return R.ok(workContactTagService.getTagList(groupId,page));
    }

    /**
     * 删除标签
     */
    @SysLog("删除标签")
    @DeleteMapping("/{tagIds}")
    @ApiOperation(value = "删除标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name="tagIds",value="需要删除的标签id",dataType="String")
    })
    public R deleteTag(@PathVariable String tagIds) {
        if (tagIds == null || tagIds.isEmpty()) {
            throw new CheckedException("标签 id 无效");
        }
        workContactTagService.deleteTag(tagIds);
        return R.ok();
    }

    /**
     * 同步标签 TODO 是否异步
     */
    @SysLog("同步标签")
    @PutMapping("/syncontacttag")
    @ApiOperation(value = "同步标签")
    public R<String> synContactTag() {
        workContactTagService.synContactTag();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return R.ok(format.format(new Date()));
    }

    /**
     * 所有标签
     */
    @GetMapping("/alltag")
    @ApiOperation(value = "所有标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="标签组id",dataType="Integer")
    })
    public R<List<ContactTagVO>> getAllTag(Integer groupId) {
        return R.ok(workContactTagService.getAllTag(groupId, CorpContextHolder.getCorpId()));
    }

    /**
     * 创建标签
     */
    @SysLog("创建标签")
    @PostMapping
    @ApiOperation(value = "创建标签")
    @ApiImplicitParams({
//            @ApiImplicitParam(name="groupId",value="标签组id",dataType="Integer"),
//            @ApiImplicitParam(name="tagName",value="标签名",dataType="String")
    })
    public R<Boolean> createTag(@RequestBody WorkContactTagAddDTO addDTO) {
        return  workContactTagService.createTag(addDTO.getGroupId(), addDTO.getTagNameList()) ? R.ok(Boolean.TRUE,"添加成功") : R.failed(Boolean.FALSE,"添加失败");
    }

    /**
     * 获取标签详情
     */
    @GetMapping("/{tagId}")
    @ApiOperation(value = "获取标签详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="tagId",value="标签id",dataType="Integer")
    })
    public R<ContactTagDetailVO> getTagDetail(@PathVariable Integer tagId) {
        if (tagId == null || tagId <= 0) {
            return R.failed("标签组id不能为空");
        }
        return R.ok(workContactTagService.getTagDetail(tagId));
    }

    /**
     * 移动标签
     */
    @SysLog("移动标签")
    @PutMapping("/move")
    @ApiOperation(value = "移动标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="新标签组id",dataType="Integer"),
            @ApiImplicitParam(name="tagId",value="标签id",dataType="String")
    })
    public R<String> moveTags(Integer groupId,String tagId) {
        if (groupId == null || groupId <= 0) {
            return R.failed("标签组 id 无效");
        }
        if (tagId == null || tagId.isEmpty()) {
            return R.failed("标签 id 不能为空");
        }
        workContactTagService.moveTags(tagId, groupId);
        return R.ok();
    }

    /**
     * 更新标签
     */
    @SysLog("更新标签")
    @PutMapping
    @ApiOperation(value = "更新标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name="tagId",value="标签id",dataType="Integer"),
            @ApiImplicitParam(name="groupId",value="新标签组id",dataType="Integer"),
            @ApiImplicitParam(name="tagName",value="标签名",dataType="String"),
            @ApiImplicitParam(name="isUpdate",value="更新标识，1--更新，非1--不更新",dataType="Integer")
    })
    public R updateTag(Integer tagId, Integer groupId,String tagName,Integer isUpdate) {
        if (tagId == null || tagId <= 0) {
            return R.failed("标签 id 无效");
        }
        if (groupId == null || groupId <= 0) {
            return R.failed("标签组 id 无效");
        }
        if (tagName == null || tagName.isEmpty()) {
            return R.failed("标签名不能为空");
        }
        if (isUpdate > 1) {
            return R.ok();
        }
        workContactTagService.updateTag(tagId, groupId, tagName, isUpdate);
        return R.ok();
    }

    @SysLog("给客户打标签")
    @PostMapping("/save")
    @ApiOperation(value = "给客户打标签")
    public R save(@RequestBody WorkContactTagsAddDTO dto){
        workContactTagService.saveLabelByConsole(dto);
        return R.ok();
    }

}
